//
//  Home2Card2TableViewCell.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/19.
//  
  

#import "Home2Card2TableViewCell.h"
#import <YYKit.h>
#import <Masonry.h>


@interface Home2Card2TableViewCell ()
@property (nonatomic, strong) UIStackView *stackView;
@end
@implementation Home2Card2TableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = UIColorHex(#1B1B1B);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.bgView];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
        
        SLOtherProductComponent *other = [SLOtherProductComponent new];
        [self.contentView addSubview:other];
        [other mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.bgView).inset(20);
            make.top.equalTo(self.bgView).offset(2);
            make.height.mas_equalTo(20+30+20);
        }];
        
        [self.contentView addSubview:self.stackView];
        [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.bgView).inset(20);
            make.top.equalTo(other.mas_bottom);
            make.bottom.equalTo(self.bgView);
        }];
    }
    return self;
}

- (void)setItems:(NSArray<NSDictionary *> *)items {
    _items = items;
    [self.stackView removeAllSubviews];
    for (NSDictionary *dict in items) {
        SLProductItemComponent *item = [SLProductItemComponent new];
        [self.stackView addArrangedSubview:item];
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(142);
        }];
        
        // 赋值
        [item.avatar sl_setImageWithURL:dict[@"nice"] options:SDWebImageRefreshCached];
        item.productName.text = dict[@"chair"];
        item.apply.text = dict[@"refuge"];
        item.creditLimitMoney.text = dict[@"ray"];
        item.creditLimitMoney.text = dict[@"supposed"];
        
        // 点击事件
        @weakify(self);
        [item jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            @strongify(self);
            !self.itemClickCallback ?: self.itemClickCallback([items indexOfObject:dict]);
        }];
        
    }
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = UIColorHex(#F5F5F5);
    }
    return _bgView;
}

- (UIStackView *)stackView {
    if (!_stackView) {
        _stackView = [UIStackView new];
        _stackView.axis = UILayoutConstraintAxisVertical;
        _stackView.spacing = 20;
    }
    return _stackView;
}

@end
